package android.bluebox.view;

import android.app.Activity;
import android.app.ProgressDialog;
import android.bluebox.R;
import android.bluebox.model.DatabaseBox;
import android.bluebox.model.KeyCrypto;
import android.bluebox.model.NetworkBox;
import android.bluebox.model.PasswordCrypto;
import android.bluebox.model.StaticBox;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginView extends Activity {

	TextView txtRemind;
	EditText edtPassword;
	
	Button btnLogin;
	Button btnClear;
	Button btnRemind;

	Intent intent;

	Context contextLogin;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		contextLogin = this;

		edtPassword = (EditText) findViewById(R.id.edtLoginPassword);

		btnLogin = (Button) findViewById(R.id.btnLoginLogin);
		btnLogin.setOnClickListener(login);

		btnClear = (Button) findViewById(R.id.btnLoginClear);
		btnClear.setOnClickListener(clear);
		
		btnRemind = (Button) findViewById(R.id.btnLoginRemind);
		btnRemind.setOnClickListener(remind);

		intent = new Intent(LoginView.this, FirstView.class);
	}

	private OnClickListener login = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			new Login().execute();
		}
	};

	private OnClickListener clear = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			edtPassword.setText("");
		}
	};
	
	private OnClickListener remind = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			txtRemind = (TextView) findViewById(R.id.txtRemind);
			txtRemind.setText(StaticBox.dbBox.getRemind());
		}
	};

	public class Login extends AsyncTask<Void, Void, Void> {

		private final ProgressDialog dialog = new ProgressDialog(LoginView.this);
		boolean successful = false;

		protected void onPreExecute() {
			this.dialog.setTitle("Login");
			this.dialog.setMessage("Please wait...");
			this.dialog.show();
		}

		@Override
		protected Void doInBackground(Void... arg0) {

			String pwd = edtPassword.getText().toString();
			StaticBox.passwordCrypto = new PasswordCrypto();
			StaticBox.passwordCrypto.init(pwd);
			boolean login = StaticBox.passwordCrypto.checkPassword(pwd);

			if (login) {

				StaticBox.dbBox = new DatabaseBox(contextLogin);
				StaticBox.netBox = new NetworkBox();
				StaticBox.keyCrypto = new KeyCrypto();
				StaticBox.keyCrypto.init(StaticBox.passwordCrypto);
				
				startActivity(intent);
				finish();

				successful = true;
			} else {
				successful = false;
			}

			return null;
		}

		protected void onProgressUpdate(final Void unused) {
		}

		protected void onPostExecute(final Void unused) {
			if (this.dialog.isShowing())
				this.dialog.dismiss();

			Toast.makeText(getBaseContext(), (successful)? "Login Successful" : "Login Unsuccessful", Toast.LENGTH_SHORT).show();
		}
	}
}
